/*
 *  Copyright (C) 2011 felixtheripper <felixtheripper_AT_gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
/**
 *
 * @author felixtheripper
 *
 * email: felixtheripper_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 *
 */


package org.myorg.ctm.etk.gui.swg.display.dialog;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;

import org.myorg.shr.gui.swg.lang.IShrObj;

/**
 *
 * 
 */

abstract public class DlgEscapeAbs extends JDialog implements
    IShrObj,
    WindowListener
{
    // ---------------
    // ABSTRACT PUBLIC

    /**
	 *
	 */
	private static final long serialVersionUID = 1L;

	abstract public boolean init();


    // ------
    // PUBLIC

    @Override
    public void setVisible(boolean bln)
    {
        if (bln)
            _prepare2show();

        super.setVisible(bln);
    }


    /**
        may be overwritten in subclasses, in case of dialog of type "choice"
        if so, disabling cancel, force to wait till user clicks on a button
    **/
    public void windowClosing(WindowEvent evt)
    {
        _cancel_();
    }

    public void windowActivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}


    // --

    public void destroy()
    {
        /* BEGIN bug fix: jdk1.3.1/Linux RH 7.2/KDE, december 25, 2001
           second attempt to show dialog causes the appli to hang!
           iconifying the main appli, followed by uniconifying will allow to show up the dialog and unfreeze the appli

           related javasoft's bug parade: Bug ID 4509276

           a simple call to "dispose()" allows to fix up this bug!
        */




        dispose();
        // END bug fix: jdk1.3.1/Linux RH 7.2/KDE


    }


    // ---------
    // PROTECTED

    protected DlgEscapeAbs()
    {
        this((Frame)null, false);
    }

    protected DlgEscapeAbs(Frame owner)
    {
        this(owner, false);
    }

    protected DlgEscapeAbs(Frame owner, boolean modal)
    {
        this(owner, null, modal);
    }

    protected DlgEscapeAbs(Frame owner, String title)
    {
        this(owner, title, false);
    }



    protected DlgEscapeAbs(Dialog owner)
    {
        this(owner, false);
    }

    protected DlgEscapeAbs(Dialog owner, boolean modal)
    {
        this(owner, null, modal);
    }

    protected DlgEscapeAbs(Dialog owner, String title)
    {
        this(owner, title, false);
    }

    // -----

    protected DlgEscapeAbs(Frame owner, String title, boolean modal)
    {
        super(owner, title, modal);
        _initialize();
    }

    protected DlgEscapeAbs(Dialog owner, String title, boolean modal)
    {
        super(owner, title, modal);
        _initialize();
    }

    // ----

    @Override
    protected JRootPane createRootPane()
    {
        ActionListener actionListener = new ActionListener()
        {
            public void actionPerformed(ActionEvent actionEvent)
            {
                _cancel_();
            }
        };

        JRootPane rpn = new JRootPane();
        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        rpn.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
        return rpn;
    }

    protected void _cancel_()
    {
        Window wnd = getOwner();
        setVisible(false);

        if (wnd != null)
        {
            wnd.paintComponents(wnd.getGraphics());
        }

        // BEG MODIF: jan 29, 2001: fix up bug with Linux RH7.2 / JVM 1.3.1
        dispose();
        // END MODIF: jan 29, 2001: fix up bug with Linux RH7.2 / JVM 1.3.1
    }

    // -------
    // PRIVATE

    private void _prepare2show()
    {
        pack();

        if (getOwner() != null)
            setLocationRelativeTo(getOwner());
    }

    private void _initialize()
    {
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

        addWindowListener(this);

    }
}
